#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"
#include "Game/GameState.h"

void LocalScene::OnInit()
{
	mCamera.LookAt(mCamera.GetPosition(), glm::vec3(40.0, 26.0, -40.0));
	mCamera.UpdateViewMatrix();
	// mCamera.SetSpeed(4.f, 8.f);

	mProcessIndex = 0;

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");

	mDeferredProcess = GetProcess<DeferredProcess>("DeferredProcess");

	// mCubeCloudProcess = GetProcess<CubeCloudProcess>("CubeCloudProcess");

	mNoiseCloudProcess = GetProcess<NoiseCloudProcess>("NoiseCloudProcess");

	mLightAngle = 0.f;

	GeometryGenerator geometryGenerator;

	GeometryGenerator::MeshData panelData = geometryGenerator.CreatePanel(10.f, 10.f, glm::vec3(0.f, 1.f, 0.f));
	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.5f, 20, 20);
	GeometryGenerator::MeshData cubeData = geometryGenerator.CreateCube(1.f, 1.f, 1.f, 1);

	LocalEntity* light1 = CreateEntity<LocalEntity>("light1");
	light1->AddComponent<Model>(sphereData);
	light1->AddComponent<LightMaterial>(glm::vec4(-2.f, 0.f, 0.f, 1.f), glm::vec4(1.f, 0.5f, 0.5f, 1.f));
	light1->GetTransform().SetLocalScale(glm::vec3(0.2f));
	light1->GetTransform().UpdateMatrices();
	light1->SetColor(glm::vec4(1.f));

	LocalEntity* light2 = CreateEntity<LocalEntity>("light2");
	light2->AddComponent<Model>(sphereData);
	light2->AddComponent<LightMaterial>(glm::vec4(2.f, 0.f, 0.f, 1.f), glm::vec4(0.f, 0.f, 1.f, 1.f));
	light2->GetTransform().SetLocalScale(glm::vec3(0.2f));
	light2->GetTransform().UpdateMatrices();
	light2->SetColor(glm::vec4(1.f));

	LocalEntity* light3 = CreateEntity<LocalEntity>("light3");
	light3->AddComponent<Model>(sphereData);
	light3->AddComponent<LightMaterial>(glm::vec4(0.f, 0.f, 0.f, 1.f), glm::vec4(1.f, 1.f, 0.f, 1.f));
	light3->GetTransform().SetLocalScale(glm::vec3(0.2f));
	light3->GetTransform().UpdateMatrices();
	light3->SetColor(glm::vec4(1.f));


	mLightEntitys.push_back(light1);
	mLightEntitys.push_back(light2);
	mLightEntitys.push_back(light3);


	LocalEntity* panelEntity = CreateEntity<LocalEntity>("panel");
	panelEntity->AddComponent<Model>(panelData);
	panelEntity->AddComponent<TextureMaterial>("Minecraft\\Bricks_Color.png", "Minecraft\\Bricks_Normal.png");
	panelEntity->SetColor(glm::vec4(0.f, 0.72f, 0.f, 1.f));

	LocalEntity* wallEntity = CreateEntity<LocalEntity>("wall");
	wallEntity->AddComponent<Model>(cubeData);
	wallEntity->AddComponent<TextureMaterial>("Minecraft\\Bricks2_Color.jpg", "Minecraft\\Bricks2_Normal.jpg");
	wallEntity->GetTransform().SetLocalPosition(glm::vec3(3.f, 15.f, 0.f));
	wallEntity->GetTransform().SetLocalScale(glm::vec3(0.5f, 30.f, 4.f));
	wallEntity->GetTransform().UpdateMatrices();
	wallEntity->SetColor(glm::vec4(0.f, 1.f, 0.f, 1.f));


	LocalEntity* hammerEntity = CreateEntity<LocalEntity>("hammer");
	hammerEntity->AddComponent<Model>("Hammer.obj");
	hammerEntity->AddComponent<TextureMaterial>("Pbr\\Hammer_Albedo.jpg", "Pbr\\Hammer_Normal.jpg");
	hammerEntity->GetTransform().SetLocalPosition(glm::vec3(-1.8f, 0.5f, 0.f));
	hammerEntity->GetTransform().SetLocalScale(glm::vec3(0.15f));
	hammerEntity->GetTransform().UpdateMatrices();
	hammerEntity->SetColor(glm::vec4(0.25f, 0.85f, 0.25f, 1.f));

	LocalEntity* stoneManEntity = CreateEntity<LocalEntity>("stoneMan");
	stoneManEntity->AddComponent<Model>("StoneMan.obj");
	stoneManEntity->AddComponent<TextureMaterial>("Pbr\\StoneMan_Albedo.jpg", "Pbr\\StoneMan_Normal.png");
	stoneManEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.f, 0.f));
	stoneManEntity->GetTransform().SetLocalScale(glm::vec3(0.2f));
	stoneManEntity->GetTransform().UpdateMatrices();
	stoneManEntity->SetColor(glm::vec4(0.25f, 0.85f, 0.25f, 1.f));
}

void LocalScene::OnExit()
{

}

void LocalScene::OnTick(float deltaTime)
{
	/*if (GGameState->GetFrameAmount() > 1)
		return;*/

	mLightAngle += deltaTime * 0.1f;

	for (uint32 i = 0; i < mLightEntitys.size(); ++i)
	{
		mLightEntitys[i]->GetTransform().SetLocalPosition(glm::vec3(glm::sin(mLightAngle + glm::radians(60.f * i)) * (8.f + i), 8.f + i, glm::cos(mLightAngle + glm::radians(60.f * i)) * (8.f - i)));
		mLightEntitys[i]->GetTransform().UpdateMatrices();
	}
}

void LocalScene::OnImgui()
{
	GameScene::OnImgui();


	ImGui::Begin("DeferredOption");

	ImGui::Text("Select Debug");

#if 0
	ImGui::RadioButton("RayMarching", &mCubeCloudProcess->mDebugIndex, 0);
	ImGui::RadioButton("Position", &mCubeCloudProcess->mDebugIndex, 1);
	ImGui::RadioButton("Normal", &mCubeCloudProcess->mDebugIndex, 2);
	ImGui::RadioButton("Albedo", &mCubeCloudProcess->mDebugIndex, 3);
	ImGui::RadioButton("Depth", &mCubeCloudProcess->mDebugIndex, 4);
#else

	ImGui::RadioButton("RayMarching", &mNoiseCloudProcess->mDebugIndex, 0);
	ImGui::RadioButton("Position", &mNoiseCloudProcess->mDebugIndex, 1);
	ImGui::RadioButton("Normal", &mNoiseCloudProcess->mDebugIndex, 2);
	ImGui::RadioButton("Albedo", &mNoiseCloudProcess->mDebugIndex, 3);
	ImGui::RadioButton("Depth", &mNoiseCloudProcess->mDebugIndex, 4);

	ImGui::Text("Light Setting");
	ImGui::DragFloat("Base Scale", &mNoiseCloudProcess->mBaseScale, 0.025f, 0.f, 100.f);
	ImGui::DragFloat("Light Scale", &mNoiseCloudProcess->mLightScale, 0.025f, 0.f, 100.f);
	ImGui::DragFloat("Final Scale", &mNoiseCloudProcess->mFinalScale, 0.025f, 0.f, 100.f);
	ImGui::DragFloat("Noise Scale", &mNoiseCloudProcess->mNoiseScale, 0.025f, 0.f, 100.f);

#endif



	ImGui::End();

}
